package cn.iocoder.yudao.module.system.controller.admin.mini.userInfo.vip;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.order.controller.admin.order.vo.OrderExcelVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.order.controller.admin.order.vo.OrderPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.order.dal.dataobject.order.OrderDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.order.dal.mysql.order.OrderMapper;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.shopGuide.dal.dataobject.shopguide.ShopGuideDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.shopGuide.dal.mysql.shopguide.ShopGuideMapper;
import cn.iocoder.yudao.module.system.controller.admin.mini.userInfo.user.dal.dataobject.user.UserDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.userInfo.user.dal.mysql.user.QdUserMapper;
import cn.iocoder.yudao.module.system.controller.admin.mini.userInfo.vip.mapper.VipMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@RestController
@RequestMapping("/qd/vip")
@Validated
@Transactional
public class VipController{

    @Resource
    QdUserMapper userMapper;
    @Resource
    VipMapper vipMapper;
    @Resource
    OrderMapper orderMapper;
    @Resource
    ShopGuideMapper shopGuideMapper;

    @PostMapping("/vipPage")
    @Operation(summary = "开通会员记录分页")
    public CommonResult vipPage(@RequestBody PageVo page){
        Page<VipDO> vipDOPage = vipMapper.selectPage(new Page<>(page.getPageNo(), page.getPageSize()), new LambdaQueryWrapperX<VipDO>()
                .betweenIfPresent(BaseDO::getCreateTime, page.getCreateTime())
                .eqIfPresent(VipDO::getPayStatus,page.getPayStatus())
                .orderByDesc(BaseDO::getCreateTime)
        );
        for (VipDO vo : vipDOPage.getRecords()) {
            UserDO user = userMapper.selectById(vo.getCreator());
            if (user!=null){
                vo.setUserPhone(user.getPhone());
                vo.setNickName(user.getNickName());
            }
        }

        return CommonResult.success(vipDOPage);
    }



    @PostMapping("/vipShopPage")
    @Operation(summary = "会员消费记录分页")
    public CommonResult vipShopPage(@RequestBody OrderPageReqVO page){
        Page<OrderDO> orderPage = orderMapper.selectPage(new Page<>(page.getPageNo(), page.getPageSize()), new LambdaQueryWrapperX<OrderDO>()
                .betweenIfPresent(OrderDO::getCreateTime, page.getCreateTime())
                .eqIfPresent(OrderDO::getPayStatus, page.getPayStatus())
                .eq(OrderDO::getIsVip, 1)
                .orderByDesc(BaseDO::getCreateTime)
        );
        List<OrderDO> list = orderPage.getRecords();
        for (OrderDO orderDO : list) {
            String phone="";
            UserDO userDO = userMapper.selectById(orderDO.getCreator());
            if (userDO!=null){
                orderDO.setPhone(phone);
                orderDO.setNickName(userDO.getNickName());
                orderDO.setVipId(userDO.getVipId());
            }
            assert userDO != null;
            if (userDO.getVipEndTime()!=null){
                ShopGuideDO shopGuideDO = shopGuideMapper.selectById(userDO.getShopUserId());
                if (shopGuideDO!=null){
                    orderDO.setShopName(shopGuideDO.getName());
                }
            }
        }
        orderPage.setRecords(list);
        return CommonResult.success(orderPage);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "消费记录-导出")
    @OperateLog(type = EXPORT)
    public void exportOrderExcel(OrderPageReqVO page,HttpServletResponse response) throws IOException {
        List<VipExcelVO> list=new ArrayList<>();

        Map<Integer,UserDO> userMap=new HashMap<>();
        for (UserDO vo : userMapper.selectList()) {
            userMap.put(vo.getId(),vo);
        }
        Map<Integer,String> shopMap=new HashMap<>();

        for (ShopGuideDO vo : shopGuideMapper.selectList()) {
            shopMap.put(vo.getId(),vo.getName());
        }

        for (OrderDO vo : orderMapper.selectList(new LambdaQueryWrapperX<OrderDO>()
                .betweenIfPresent(OrderDO::getCreateTime, page.getCreateTime())
                .eqIfPresent(OrderDO::getPayStatus, page.getPayStatus())
                .eq(OrderDO::getIsVip, 1).orderByDesc(BaseDO::getCreateTime))) {
            Integer userId=Integer.parseInt(vo.getCreator());
            UserDO user = userMap.get(userId);
            String userName=user.getNickName();
            String personName="";
            if (user.getShopUserId()!=null){
                personName = shopMap.get(user.getShopUserId());
            }
            list.add(new VipExcelVO(vo,userName,personName));
        }
        ExcelUtils.write(response, "消费记录导出.xls", "数据", VipExcelVO.class, list);
    }
}